import { defineComponent, ref, reactive } from "vue";
import TablePage from "@/components/TablePage/TablePage";
import { tablesInterface } from "@/common/interface/table.interface";
import NoticeService from "./index.service";
import StatusTag from "@/components/StatusTag/StatusTag";
import styles from "./index.module.less";
import Add from "./Add/Add";
export default defineComponent({
  name: "Notice",
  setup() {
    const tablePageRef = ref(null as any);
    const noticeService = new NoticeService();
    // 表格定义
    const tables = reactive({
      list: [
        {
          title: "公告管理",
          rowKey: "noticeId",
          columns: [
            {
              title: "ID",
              dataIndex: "noticeId",
              align: "center",
              width: "80px",
            },
            {
              title: "公告标题",
              dataIndex: "noticeTitle",
              align: "center",
              width: "180px",
            },
            {
              title: "公告内容",
              dataIndex: "noticeText",
              align: "center",
              width: "280px",
            },
            {
              title: "排序",
              dataIndex: "sort",
              align: "center",
              width: "180px",
            },
            {
              title: "状态",
              dataIndex: "status",
              align: "center",
              width: "180px",
            },
            {
              title: "备注",
              dataIndex: "remark",
              align: "center",
              width: "280px",
            },
            {
              title: "创建时间",
              dataIndex: "createTime",
              align: "center",
              width: "280px",
            },
          ],
          toolbar: {
            left: ["refresh", "add"],
            right: ["filter", "search"],
            add: {
              permissions: "music:notice:add",
              handler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'add' });
              }
            },
          },
          searchForm: {
            noticeTitle: {
              type: "input",
              label: "公告标题",
              attrs: { placeholder: "公告标题", allowClear: true },
            },
            status: {
              type: "select",
              label: "状态",
              attrs: { placeholder: "状态", allowClear: true },
              datas: [{label: "开启",value: "0"},{label: "关闭", value: "1"}],
            }
          },
          service: noticeService,
          operation: ({record}) => [
            {
              type: "edit",
              permissions: "music:notice:edit",
              clickHandler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'edit', noticeId: record.noticeId });
              }
            },
            {
              type: "delete",
              permissions: "music:notice:remove",
              clickHandler: "delete",
            },
          ],
          bodyCell: ({ text, record, index, column }) => {
            if (column.dataIndex === "noticeTitle") {
              return <>
                <a-tooltip v-slots={{title: () => <span>{text}</span>}}>
                  <div class={styles.noticeTitle}>{text}</div>
                </a-tooltip>
              </>
            }
            if (column.dataIndex === "noticeText") {
              return <>
                <a-tooltip v-slots={{title: () => <span>{text}</span>}}>
                  <div class={styles.noticeText}>{text}</div>
                </a-tooltip>
              </>
            }
            if (column.dataIndex === "status") {
              return <>
                <StatusTag text={text} />
              </>
            }
          },
          popups: [
            {
              ref: "addRef",
              dom: () => <Add onChange={() => { tablePageRef.value.reload(); }} />,
            }
          ],
          pagination: true,
          tableSourceFormat: {
            data: 'rows',
            pageSize: 'pageSize',
            current: 'pageNum',
          },
        }
      ],
    } as tablesInterface)
    // 配置项
    const config = reactive({
      table: tables.list[0],
    })
    return () => (
      <TablePage ref={tablePageRef} config={config} />
    )
  }
})